package com.dd.stack.mybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.stack.common.util.Result;
import com.dd.stack.common.util.ResultEnum;
import com.dd.stack.common.util.annotation.Attention;
import com.dd.stack.common.util.response.ApiResponse;
import com.dd.stack.mybatisplus.mapper.DataObjectMapper;
import com.dd.stack.mybatisplus.pojo.DO.DataObjectDO;
import com.dd.stack.mybatisplus.pojo.VO.DataObjectVO;
import com.dd.stack.mybatisplus.pojo.VO.TypeEnumVO;
import com.dd.stack.mybatisplus.pojo.req.DataObjectAddReq;
import com.dd.stack.mybatisplus.pojo.req.DataObjectBatchQueryReq;
import com.dd.stack.mybatisplus.pojo.req.DataObjectQueryReq;
import com.dd.stack.mybatisplus.pojo.req.DataObjectUpdateReq;
import com.dd.stack.mybatisplus.service.inner.DataObjectBatchService;
import com.dd.stack.mybatisplus.service.inner.DataObjectService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/1/8 22:46
 * @Description DataObjectController
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/DO")
@Api(tags = "DataObject单表操作")
public class DataObjectController {

    private final DataObjectService dataObjectService;
    private final DataObjectMapper dataObjectMapper;
    private final DataObjectBatchService dataObjectBatchService;

    @PostMapping("/add")
    @ApiOperation("添加") // TODO 测试使用循环添加和batch添加所用时长的差别
    public ApiResponse<Void> add(@ApiParam("DataObjectDO插入数据请求类") @RequestBody DataObjectAddReq req){
        log.info("C DataObjectController M add() req = {}", req);
        dataObjectService.addOne(req);
        return ApiResponse.success();
    }

    @PostMapping("/add/batch/mp")
    @Attention("第一次测试添加时间为45ms 数量2")
    @ApiOperation("批量添加（使用MyBatis-Plus提供的批量插入操作）")
    public ApiResponse<Void> addBatchMp(@ApiParam("DataObjectDO插入数据请求类") @RequestBody List<DataObjectDO> req){
        log.info("C DataObjectController M addBatchMp() req = {}", req);
        // 获取执行结果并返回
        dataObjectBatchService.saveBatch(req);
        return ApiResponse.success();
    }

    @PostMapping("/add/batch/custom")
    @Attention("第一次测试添加时间为995ms 数量2")
    @ApiOperation("批量添加（自定义的插入操作 -> 真正意义的批量处理）")
    public ApiResponse<Void> addBatchCustom(@ApiParam("DataObjectDO插入数据请求类") @RequestBody List<DataObjectDO> req){
        log.info("C DataObjectController M addBatchCustom() req = {}", req);
        // 获取执行结果并返回
        dataObjectBatchService.saveBatchByCustom(req);
        return ApiResponse.success();
    }

    @PatchMapping("/update")
    @ApiOperation("DataObjectDO更新数据")
    public ApiResponse<Void> update(@ApiParam("DataObjectDO更新数据请求类") @RequestBody DataObjectUpdateReq req){
        log.info("C DataObjectController M update() req = {}", req);
        dataObjectService.updateOne(req);
        return ApiResponse.success();
    }

    @PostMapping("/page")
    @ApiOperation("DataObjectDO多条件范围分页查询")
    public ApiResponse<IPage<DataObjectVO>> page(@ApiParam("DataObjectDO多条件范围分页查询请求类") @RequestBody DataObjectQueryReq req){
        log.info("C DataObjectController M page() req = {}", req);
        return ApiResponse.success(dataObjectService.pageQuery(req));
    }

    @GetMapping("/ids")
    @ApiOperation("根据ids批量查询DataObjectDO")
    public ApiResponse<List<DataObjectDO>> selectByIds(@ApiParam("批量查询DataObjectDO Req")
                                                  @RequestBody DataObjectBatchQueryReq req){
        return ApiResponse.success(dataObjectMapper.selectByIds(req.getIds()));
    }
}
